#include<bits/stdc++.h>
#define ll long long
#define un unsigned
#define INF 214748364719260817ll
using namespace std;
ll n;
struct px
{
	ll x,y;
	bool operator<(const px&b)const
	{
		return (x^b.x)?x<b.x:y<b.y;
	}
}a[500005];
struct dsu
{
	ll f[500005];
	void init()
	{
		for(ll i=1;i<=n;++i)f[i]=i;
	}
	ll find(ll x)
	{
		return x==f[x]?x:f[x]=find(f[x]);
	}
	void merge(ll x,ll y)
	{
		f[find(x)]=find(y);
	}
}d;
map<px,ll>s;
ll sz[500005];
ll to[500005];
vector<ll>ss[500005];
map<ll,map<ll,ll>>e;
ll head[500005],cnt;
struct ed
{
	ll v,next;
}edge[1000005];
void add(ll u,ll v)
{
	edge[++cnt].v=v;edge[cnt].next=head[u];head[u]=cnt;
	edge[++cnt].v=u;edge[cnt].next=head[v];head[v]=cnt;
}
ll res;
void dfs(ll id,ll fa)
{
	for(ll i=head[id];i;i=edge[i].next)
	{
		ll v=edge[i].v;
		if(v==fa)continue;
		dfs(v,id);
		sz[id]+=sz[v];
	}
	res+=sz[id]*(n-sz[id]);
}
int main()
{
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(ll i=1;i<=n;++i)cin>>a[i].x>>a[i].y,s[a[i]]=i;
	d.init();
	for(ll i=1;i<=n;++i)
	{
		px ss=(px){a[i].x+1,a[i].y};
		if(s.count(ss))
		d.merge(s[ss],i);	
	}
	ll cot=0;
	for(ll i=1;i<=n;++i)
	{
		if(!to[d.find(i)])
			to[d.find(i)]=++cot;
		++sz[to[d.find(i)]];
		ss[to[d.find(i)]].emplace_back(i);
	}
	for(ll i=1;i<=cot;++i)
	{
		for(auto id:ss[i])
		{
			px ss=(px){a[id].x,a[id].y+1};
			if(s.count(ss))
				e[i][to[d.find(s[ss])]]=1;
		}		
	}
	for(auto u:e)
		for(auto v:u.second)
		add(u.first,v.first);
	dfs(1,0);
	d.init();
	for(ll i=1;i<=cot;++i)head[i]=sz[i]=0,ss[i].clear();
	cot=cnt=0;
	memset(to,0,sizeof(to));
	e.clear();
	for(ll i=1;i<=n;++i)
	{
		px ss=(px){a[i].x,a[i].y+1};
		if(s.count(ss))
		d.merge(s[ss],i);	
	}
	for(ll i=1;i<=n;++i)
	{
		if(!to[d.find(i)])
			to[d.find(i)]=++cot;
		++sz[to[d.find(i)]];
		ss[to[d.find(i)]].emplace_back(i);
	}
	for(ll i=1;i<=cot;++i)
	{
		for(auto id:ss[i])
		{
			px ss=(px){a[id].x+1,a[id].y};
			if(s.count(ss))
				e[i][to[d.find(s[ss])]]=1;
		}		
	}
	for(auto u:e)
		for(auto v:u.second)
		add(u.first,v.first);
	dfs(1,0);
	cout<<res;
}
